package com.zyajac.springBootDemo.service.Impl;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.zyajac.springBootDemo.commonUtils.CommonUtils;
import com.zyajac.springBootDemo.commonUtils.EncryptionUtils.Md5Utils;
import com.zyajac.springBootDemo.dao.DaoSupport;
import com.zyajac.springBootDemo.entity.User;
import com.zyajac.springBootDemo.enums.ResultEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import user.UserService;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;


@Service
@org.apache.dubbo.config.annotation.Service(version = "1.0.0")
public class UserServiceImpl  implements UserService {

    @Resource(name = "DaoSupport")
    private DaoSupport dao;


    /**
      *
      * @author LZG
      * @date  2019/8/11
      * @param  params 参数
      * @return 新增 结果
      **/
    @Override
    public Map<String  , Object > save(String params){
        if(params == null || StringUtils.isBlank(params))  return CommonUtils.setResult(ResultEnum.BLANK_PARAM, "用户参数为空");


        User  user ;
        try{
            JSONObject paramJSON = JSONObject.parseObject(params);
            user = JSONObject.parseObject(paramJSON.getString("User"), User.class);
        }
        catch (JSONException e){
            e.printStackTrace();
            return CommonUtils.setResult(ResultEnum.JSON_ERROR);
        }

        int result = dao.insert("UserMapper.", user);
        if(result  <=   0 ) return CommonUtils.setResult(ResultEnum.SQL_ERROR);

        return  CommonUtils.setResult(ResultEnum.OK);
    }


    /**
      * 批量新增
      * @author LZG
      * @date  2019/8/11
      * @param  params 参数
      * @return 新增结果
      **/

    @Override
    public Map<String  , Object> saveBatch(String params){
        if(params == null || StringUtils.isBlank(params))  return CommonUtils.setResult(ResultEnum.BLANK_PARAM, "用户参数为空");

        List<User> users;
        try{
            JSONObject paramJSON = JSONObject.parseObject(params);
            users = JSONObject.parseArray(paramJSON.getString("UserList"), User.class);
        }catch (JSONException e){
            e.printStackTrace();
            return CommonUtils.setResult(ResultEnum.JSON_ERROR);
        }

        int insert = dao.insert("UserMapper.", users);

        if(insert >=  0 ) return CommonUtils.setResult(ResultEnum.SQL_ERROR);

        return CommonUtils.setResult(ResultEnum.OK);
    }


    /**
      * 修改
      * @author LZG
      * @date  2019/8/11
      * @param  params 参数
      * @return 新增结果
      **/
    @Override
    public Map<String  , Object> update(String params){
        if(params == null || StringUtils.isBlank(params))  return CommonUtils.setResult(ResultEnum.BLANK_PARAM, "修改用户参数为空");

        User user;
        try{
            JSONObject paramJSON = JSONObject.parseObject(params);
            user = JSONObject.parseObject(paramJSON.getString("User"), User.class);
        }catch (JSONException e){
            e.printStackTrace();
            return CommonUtils.setResult(ResultEnum.JSON_ERROR);
        }

        int insert = dao.update("UserMapper.", user);

        if(insert >=  0 ) return CommonUtils.setResult(ResultEnum.SQL_ERROR);

        return CommonUtils.setResult(ResultEnum.OK);
    }


    /**
      * 停用 用户
      * @author LZG
      * @date  2019/8/11
      * @param  params 参数
      * @return 新增结果
      **/
    @Override
    public Map<String  , Object> disableUserAccount(String params){
        if(params == null || StringUtils.isBlank(params))  return CommonUtils.setResult(ResultEnum.BLANK_PARAM, "用户参数为空");

        String userId;
        try{
            JSONObject paramJSON = JSONObject.parseObject(params);
            userId = paramJSON.getString("UserId");
        }catch (JSONException e){
            e.printStackTrace();
            return CommonUtils.setResult(ResultEnum.JSON_ERROR);
        }

        int insert = dao.insert("UserMapper.", userId);

        if(insert >=  0 ) return CommonUtils.setResult(ResultEnum.SQL_ERROR);

        return CommonUtils.setResult(ResultEnum.OK);
    }




    /**
     * 查询用户
     * @author LZG
     * @date  2019/8/11
     * @param  params 参数
     * @return 新增结果
     **/
    @Override
    public Map<String  , Object> findById(String params){
        if(params == null || StringUtils.isBlank(params))  return CommonUtils.setResult(ResultEnum.BLANK_PARAM, "查询用户参数为空");

        String userId;
        try{
            JSONObject paramJSON = JSONObject.parseObject(params);
            userId  = paramJSON.getString("User");
        }catch (JSONException e){
            e.printStackTrace();
            return CommonUtils.setResult(ResultEnum.JSON_ERROR);
        }

        User user = dao.select("UserMapper.", userId);

        if(user ==  null ) return CommonUtils.setResult(ResultEnum.SQL_ERROR);

        Map<String, Object> result = CommonUtils.setResult(ResultEnum.OK);

        result.put("User" ,  user);

        return result;
    }


    /**
     * 模糊查询 空置为findAll
     * @author LZG
     * @date  2019/8/11
     * @param  params 参数
     * @return 新增结果
     **/
    @Override
    public Map<String  , Object> findFS(String params){
        if(params == null || StringUtils.isBlank(params))  return CommonUtils.setResult(ResultEnum.BLANK_PARAM, "查询用户参数为空");

        User user;
        try{
            JSONObject paramJSON = JSONObject.parseObject(params);
            user = JSONObject.parseObject(paramJSON.getString("User"), User.class);
        }catch (JSONException e){
            e.printStackTrace();
            return CommonUtils.setResult(ResultEnum.JSON_ERROR);
        }

        List<User> userList = dao.selectList("UserMapper.", user);

        if(userList.size() <= 0 ) return CommonUtils.setResult(ResultEnum.SQL_ERROR);

        Map<String, Object> result = CommonUtils.setResult(ResultEnum.OK);

        result.put("UserList" , userList);

        return result;
    }



    /**
     * 比较账户名 密码 for Server
     * @author LZG
     * @date  2019/8/11
     * @param  username  password 参数
     * @return 新增结果
     **/
    @Override
    public boolean verifyNamAndPW(String username, String password){

        Map<String, Object> result = findFS(Collections.singletonMap("username", username).toString());

        User user = null;
        try {
            if (ResultEnum.OK.state.equals(result.get("state"))) {
                user = ((List<User>) result.get("userList")).get(0);
            }
        }catch (Exception  e){e.printStackTrace(); return  false;}

        return Md5Utils.verifyMD5WithSalt(password , user.getPassword());
    }


}
